package com.example.myx264test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;
import android.view.View;

//public class AutoFitTextureView extends TextureView {
//    private static final String TAG = "AutoFitTextureView";
//
//    private int mRatioW = 0;
//    private int mRatioH = 0;
//    private double mAspectRatio = 0.0;
//    private boolean isSetAspectRatio = false;
//
//    public AutoFitTextureView(Context context) {
//        super(context);
//    }
//
//    public AutoFitTextureView(Context context, AttributeSet attrs) {
//        super(context, attrs);
//    }
//
//    public AutoFitTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//
////    /**
////     * 设置宽高比
////     * @param width
////     * @param height
////     */
//    public void setAspectRatio(int width, int height){
//        if (width < 0 || height < 0) {
//            throw new IllegalArgumentException("width or height can not be negative.");
//        }
//        mRatioW = width;
//        mRatioH = height;
//        Log.e(TAG, "setAspectRatio: ratioW = "+mRatioW+" ratioH = "+mRatioH );
//        //请求重新布局
//        requestLayout();
//    }
//
//    public void setAspectRatio(double ratio){
//        Log.e(TAG, "setAspectRatio: ratio = "+ratio );
//        if (Math.abs(mAspectRatio - ratio) < 0.0001 ){
//            return;
//        }
//        mAspectRatio = ratio;
//        isSetAspectRatio = true;
//        //请求重新布局
//        requestLayout();
//    }
//
////    @Override
////    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
////        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
////        int width = View.MeasureSpec.getSize(widthMeasureSpec);
////        int height = View.MeasureSpec.getSize(heightMeasureSpec);
////        Log.e(TAG, "onMeasure: width = "+width+" height = "+height +", mRatioW = "+mRatioW+", mRatioH = "+mRatioH);
////        if (0 == mRatioW || 0 == mRatioH){
////            //未设定宽高比，使用预览窗口默认宽高，当AutoFitTextureView初始化的时候会走一次这个逻辑
////            setMeasuredDimension(width, height);
////            Log.e(TAG, "onMeasure 000: width = "+width+", height = "+height );
////        } else {
////            // 始终保持宽为手机的宽，这样视觉效果好
////            double ratio = ((double) mRatioW / (double)mRatioH);
////            Log.e(TAG, "onMeasure: ratio = "+ratio );
////            height = (int) ((double)width / ratio);
////            setMeasuredDimension(width, height);
////            Log.e(TAG, "onMeasure 2222: width = "+width+", height = "+height+", ratio = "+ratio);
////        }
////    }
//
//    private final int [] measure_spec = new int[2];
//    @Override
//    protected void onMeasure(int widthSpec, int heightSpec) {
//        Log.e(TAG, "onMeasure: " + widthSpec + " x " + heightSpec);
//        getMeasureSpec(measure_spec, widthSpec, heightSpec);
//        super.onMeasure(measure_spec[0], measure_spec[1]);
//    }
//
//    public void getMeasureSpec(int [] spec, int widthSpec, int heightSpec) {
//        Log.e(TAG, "getMeasureSpec");
//        if( !isSetAspectRatio ) {
//            Log.e(TAG, "doesn't have aspect ratio");
//            spec[0] = widthSpec;
//            spec[1] = heightSpec;
//            return;
//        }
//        double aspect_ratio = mAspectRatio;
//
//        int previewWidth = MeasureSpec.getSize(widthSpec);
//        int previewHeight = MeasureSpec.getSize(heightSpec);
//
//        if( previewHeight > previewWidth / aspect_ratio ) {
//            previewHeight = (int) ((double) previewWidth / aspect_ratio);
//        }
//        else {
//            previewWidth = (int) ((double) previewHeight * aspect_ratio);
//        }
//        spec[0] = MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY);
//        spec[1] = MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY);
//        Log.d(TAG, "return: " + spec[0] + " x " + spec[1]);
//    }
//
//}


public class AutoFitTextureView extends TextureView {

    private int ratioW = 0;
    private int ratioH = 0;

    public AutoFitTextureView(Context context) {
        super(context);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置宽高比
     * @param width
     * @param height
     */
    public void setAspectRatio(int width, int height){
        Log.e("TAG", "setAspectRatio: width = "+width+", height = "+height);
        if (width < 0 || height < 0){
            throw new IllegalArgumentException("width or height can not be negative.");
        }
        //相机输出尺寸宽高默认是横向的，屏幕是竖向时需要反转
        // （后续适配屏幕旋转时会有更好的方案，这里先这样）
        ratioW = width;
        ratioH = height;
        //请求重新布局
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("TAG", "onMeasure: widthMeasureSpec = "+widthMeasureSpec+", heightMeasureSpec = "+heightMeasureSpec );
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (0 == ratioW || 0 == ratioH){
            Log.e("TAG", "onMeasure: 0 == ratioW || 0 == ratioH");
            //未设定宽高比，使用预览窗口默认宽高
            setMeasuredDimension(width, height);
        }else {
            //设定宽高比，调整预览窗口大小（调整后窗口大小不超过默认值）
            if (width < height * ratioW / ratioH){
                Log.e("TAG", "onMeasure: width < , width = "+width+", height = "+(height * ratioW / ratioH));
                setMeasuredDimension(width, width * ratioH / ratioW);
            } else {
                Log.e("TAG", "onMeasure: width >= , width = "+(height * ratioW / ratioH)+", height = "+height);
                setMeasuredDimension(height * ratioW / ratioH, height);
            }
        }

    }
}
